const fs = require('fs')
const path = require('path')

/**
 * travel all the files then return file paths in Array
 * @param dir directory to be travelled
 */
function travelFile (dir) {
    return new Promise((resovle, reject) => {
        fs.readdir(dir, async (err, files) => {
            if (err) {
                return console.error(err);
            }
            const promise = files.map(filename=>{
                const fullPath = path.join(dir,filename)
                return new Promise(resolve1 => {
                    fs.stat(fullPath,async (err,stats)=>{
                        if (err) {
                            return console.error(err);
                        }
                        if(stats.isDirectory()){
                            const child = await travelFile(fullPath)
                            resolve1(...child)
                        }else{
                            resolve1(fullPath)
                        }
                    })
                })
            })
            Promise.all(promise).then(res=>{
               resovle(res.filter(item=>item))
            })
        })
    })
}
module.exports.travelFile = travelFile

/**
 * travel all the files then return file contents in Array
 * @param dir directory to be travelled
 */
async function  travelAndReadFile(dir){
    const files = await travelFile(dir)
    const promise = files.map(file=>{
        return new Promise((resolve => {
            readFile(file,content=>{
                resolve({file, content})
            })
        }))
    })
    return Promise.all(promise).then(res=>{
       return res
    })
}
module.exports.travelAndReadFile = travelAndReadFile

/**
 * write content to file
 * @param dir       file to write,create if not exist
 * @param content   content to write
 */
 function writeFile(dir, content) {
    return new Promise((resolve => {
        fs.writeFile(dir, content, function(err) {
            if (err) {
                resolve({
                    type:'error',
                    msg:err
                })
            }
            resolve({
                type:'success',
                msg:dir
            })
        });
    }))
}
module.exports.writeFile = writeFile

/**
 * delete the file
 * @param dir  the file  to delete
 */
function deleteFile(dir) {
    return new Promise((resolve => {
        fs.unlink(dir, function(err) {
            if (err) {
                resolve({
                    type:'error',
                    msg:err
                })
            }
            resolve({
                type:'success',
                msg:dir
            })
        });
    }))
}
module.exports.deleteFile = deleteFile

/**
 * read the provided file dir content
 * @param dir  the file  to read
 * @param resolve  action to do after read
 */
function readFile(dir, resolve) {
    fs.readFile(dir, 'utf-8', function(err,data) {
        if (err) {
            resolve('error')
        }
        resolve(data)
    });
}
module.exports.readFile = readFile

